﻿using Radius.NET.Interface.Kingdee.Service.Production.Dto.PRD_INSTOCK;
using Radius.NET.Interface.Kingdee.Service.Production.Dto.Qtckd;
using Radius.NET.Interface.Kingdee.Service.Production.Dto.Qtrkd;
using System.Linq;
using static SKIT.FlurlHttpClient.Wechat.Api.Models.SemanticSemproxySearchResponse.Types;
using Admin.NET.Application.Util.Extension;
using static Radius.NET.Interface.Kingdee.Service.StockWareModule.Dto.KindeeStockWareDto;
using Radius.NET.Interface.Kingdee.Attributes;
using Radius.NET.Interface.Kingdee.Entity;

namespace Radius.NET.Interface.Kingdee.Service.Production
{
    /// <summary>
    /// 金蝶生产相关DTO
    /// </summary>
    public class KindeeIssueDto
    {
        #region 生成生产入库单保存json
        /// <summary>
        /// 生成生产入库单保存json
        /// </summary>
        /// <param name="item">仓库单据实体</param>
        /// <param name="issue">工程单</param>
        /// <param name="evidenceTZ">入库申请单</param>
        /// <param name="pRD_MORPT">云星空生产汇报单</param>
        /// <param name="pRD_MO">云星空生产订单</param>
        /// <returns></returns>
        public static object BuildScrkJsonData(
            InventoryEvidenceTZ item,
            MaterialIssuePlan issue,
            InventoryEvidenceTZ evidenceTZ,
            PRD_MORPT_Dto pRD_MORPT, PRD_MO_Dto pRD_MO)
        {
            Log.Information(JsonConvert.SerializeObject(item));
            Log.Information(JsonConvert.SerializeObject(issue));
            Log.Information(JsonConvert.SerializeObject(evidenceTZ));

            var obj = new PRD_INSTOCK()
            {
                NeedUpDateFields = new List<string>(),
                NeedReturnFields = new List<string>(),
                IsDeleteEntry = true,
                SubSystemId = "",
                IsVerifyBaseDataField = false,
                IsEntryBatchFill = true,
                ValidateFlag = true,
                NumberSearch = true,
                InterationFlags = "",
                Model = new Dto.PRD_INSTOCK.Model()
                {
                    FID = 0,  //实体主键
                    FBillNo = item.Evidence_Number,
                    FBillType = new()
                    {
                        FNUMBER = "SCRKD01_SYS"  //单据类型
                    },
                    FDate = item.Date, //单据日期
                    FStockOrgId = new()
                    {
                        FNumber = KingdeeConfigConst.ORGID    //入库组织--半成品生产订单明细--入库组织
                    },
                    FStockId0 = new()
                    {
                        FNumber = "" //仓库--无需传值
                    },
                    FPrdOrgId = new()
                    {
                        FNumber = KingdeeConfigConst.ORGID //生产组织 -- 半成品生产订单表头-生产组织
                    },
                    FWorkShopId = new()
                    {
                        FNumber = "" //车间--无需传值
                    },
                    FOwnerTypeId0 = "BD_OwnerOrg", //货主类型
                    FOwnerId0 = new()
                    {
                        FNumber = KingdeeConfigConst.ORGID //货主--半成品生产订单明细-入库货主
                    },
                    FSTOCKERID = new()
                    {
                        FNAME = ""   //仓管员--无需传值
                    },
                    FDescription = "",  //备注--无需传值
                    FIsEntrust = false,  //组织委托加工--半成品生产订单表头-组织受托加工
                    FCurrId = new()
                    {
                        FNumber = ""
                    },
                    FEntrustInStockId = 0,
                    FScanBox = "",
                    F_GDRJ_PrintDateTime = "1900-01-01",
                    F_GDRJ_PrintTimes = 0,
                    F_GDRJ_CheckBox = "false",
                    FEntity = new List<Dto.PRD_INSTOCK.FEntityItem>()
                }
            };

            foreach (var el in item.FG_InvDetail)
            {
                obj.Model.FBillType.FNUMBER = "SCRKD02_SYS";
                Dto.PRD_INSTOCK.FEntityItem child = new()
                {
                    FEntryID = 0,   //实体主键
                    FSrcEntryId = pRD_MORPT.FEntity_FEntryID,
                    //Convert.ToInt32(kingdeeIssue["FTreeEntity_FEntryId"]),//源单分录内码--半成品生产订单明细分录ID
                    FIsNew = false,   //是否新增行，默认false
                    FMaterialId = new()
                    {
                        FNumber = el.Inventory_ID   //物料编码--半成品生产订单明细--物料编码
                    },
                    FAuxpropId = new()
                    {
                        FAUXPROPID__FF100002 = new()
                        {
                            FNumber = el.JarModelID //包装物及包装规格
                        },
                        FAUXPROPID__FF100003 = new()
                        {
                            FNumber = el.CF02  //出胶规格
                        },
                        FAUXPROPID__FF100004 = new()
                        {
                            FNumber = el.CF03  //地台板规格
                        }
                    },
                    FInStockType = 1,
                    FProductType = 1,   //产品类型--默认1
                    FCheckProduct = false, //产品检验--默认false
                    FUnitID = new()
                    {
                        FNumber = el.FG_Inventory is not null ? el.FG_Inventory.Unit : "GONGJIN01"  //半成品生产订单明细--单位
                    },
                    FMustQty = el.Actual_Qty,  //应收数量
                    FRealQty = el.Actual_Qty,    //实收数量
                    FMoBillNo = issue.No.Split("_")[0],  //生产订单编号---半成品生产订单表头--单据编号
                    FCostRate = 100,  //成本权重--默认100
                    FBaseUnitId = new()
                    {
                        FNumber = el.FG_Inventory is not null ? el.FG_Inventory.Unit : "GONGJIN01"   //基本单位--半成品生产订单明细--基本单位
                    },
                    FBaseMustQty = el.Actual_Qty,    //基本单位应收数量---同实收数量
                    FBaseRealQty = el.Actual_Qty,    //基本单位库存数量---同实收数量
                    FOwnerTypeId = "BD_OwnerOrg",   //货主类型
                    FOwnerId = new()
                    {
                        FNumber = KingdeeConfigConst.ORGID  //货主--半成品生产订单明细--入库货主
                    },
                    FStockId = new()
                    {
                        FNumber = item.DestStockID  //仓库
                    },
                    FStockLocId = new()
                    {
                        FSTOCKLOCID__FF100006 = new()
                        {
                            FNumber = el.Stk_id //仓位
                        },
                        FSTOCKLOCID__FF100002 = new()
                        {
                            FNumber = el.Stk_id //仓位
                        }
                    },
                    FBomId = new()
                    {
                        FNumber = ""
                    },
                    FLot = new()
                    {  //批号
                        FNumber = issue.SummaryProject
                    },
                    FISBACKFLUSH = true,   //倒冲领料---默认false
                    FMtoNo = "",
                    FProjectNo = "",
                    FWorkShopId1 = new()
                    {
                        FNumber = evidenceTZ.StockID  //半成品生产订单明细--生成车间
                    },
                    FShiftGroupId = new()
                    {
                        FNumber = ""
                    },
                    FMoId = pRD_MO.FID,  //生产订单内码--半成品生产订单表头主键ID
                    FMoEntryId = pRD_MO.FTreeEntity_FEntryId, //生产订单分录内码 --- 半成品生产订单明细分录ID
                    FMoEntrySeq = issue.No.Split("_")[1], //生产订单行号--半成品生产订单明细--序号
                    FMemo = "",
                    FStockUnitId = new()
                    {
                        FNumber = el.FG_Inventory is not null ? el.FG_Inventory.Unit : "GONGJIN01"
                    },
                    FStockRealQty = el.Actual_Qty,//库存单位实收数量
                    FSecUnitId = new()
                    {
                        FNumber = ""
                    },
                    FSecRealQty = 0,//辅助单位实收数量
                    FSrcBillType = "PRD_MORPT",//源单类型--默认值：PRD_MO 生产汇报单formid
                    FSrcBillNo = pRD_MORPT.FBillNo,//源单编号--生产汇报单单号
                    FSrcInterId = pRD_MORPT.FID, //源单内码--生产汇报单主键ID
                    FBasePrdRealQty = el.Actual_Qty, //基本单位生产实收数量
                    FIsFinished = false,//完工--默认false
                    FStockStatusId = new()
                    {
                        FNumber = "KCZT01_SYS" //库存状态--默认值：KCZT01_SYS
                    },
                    FSrcEntrySeq = Convert.ToInt32(issue.No.Split("_")[1]),//源单行号--半成品生产订单明细--序号  生产汇报单行号
                    FMOMAINENTRYID = pRD_MO.FTreeEntity_FEntryId,
                    FKeeperTypeId = "BD_KeeperOrg", //保管者类型--默认值：BD_OwnerOrg
                    FKeeperId = new()
                    {
                        FNumber = KingdeeConfigConst.ORGID
                    },
                    FProduceDate = "",
                    FSelReStkQty = 0,
                    FExpiryDate = "",
                    FBaseSelReStkQty = 0,
                    FSrcBusinessType = "",
                    FIsOverLegalOrg = false,//组织间结算跨法人--默认false
                    FSendRowId = "",
                    FSerialSubEntity = new List<Dto.PRD_INSTOCK.FSerialSubEntityItem>()
                    {
                        new ()
                        {
                            FDetailID=0,
                            FSerialNo="",
                            FSerialId = new()
                            {
                                FNumber=""
                            },
                            FSerialNote=""
                        }
                    },
                    FEntity_Link = new List<Dto.PRD_INSTOCK.FEntity_LinkItem>() {
                        new()
                        {
                            FEntity_Link_FFlowId= "f6e6eec3-5267-4f02-8593-b633da508a72",//默认空值
                            FEntity_Link_FFlowLineId= 5,//默认0
                            FEntity_Link_FRuleId= "PRD_MORPT2INSTOCK",//转换规则--默认值：0e84c6c0-1448-4030-b226-cda718614055
                            FEntity_Link_FSTableId= 0,//源单表内码--默认空值
                            FEntity_Link_FSTableName= "T_PRD_MORPTENTRY",//源单表
                            FEntity_Link_FSBillId = pRD_MORPT.FID, //源单内码--生产汇报单表头主键ID
                            FEntity_Link_FSId= pRD_MORPT.FEntity_FEntryID,//源单分录内码--生产汇报单明细分录ID
                            FEntity_Link_FBasePrdRealQtyOld = el.Actual_Qty,  //原始携带量--同实收数量
                            FEntity_Link_FBasePrdRealQty = el.Actual_Qty //修改携带量--同实收数量
                        }
                    }
                };
                obj.Model.FEntity.Add(child);
            };
            return obj;
        }
        #endregion

        #region 生成其他入库单保存json
        /// <summary>
        /// 生成其他入库单保存json
        /// </summary>
        /// <returns></returns>
        public static object BuildQtrkdJsonData(InventoryEvidence item,
            MaterialIssuePlan issue, PRD_MO_Dto pRD_MO)
        {
            STK_MISCELLANEOUS obj = new()
            {
                NeedUpDateFields = new List<string>(),
                NeedReturnFields = new List<string>(),
                IsDeleteEntry = "true",
                SubSystemId = "",
                IsVerifyBaseDataField = "false",
                IsEntryBatchFill = "true",
                ValidateFlag = "true",
                NumberSearch = "true",
                InterationFlags = "",
                Model = new Dto.Qtrkd.Model()
                {
                    FID = 0,
                    FBillNo = item.Evidence_Number,
                    FBillTypeID = new Dto.Qtrkd.FBillTypeID()
                    {
                        FNUMBER = "QTRKD01_SYS"
                    },
                    FStockOrgId = new Dto.Qtrkd.FStockOrgId()
                    {
                        FNumber = KingdeeConfigConst.ORGID
                    },
                    FStockDirect = "",
                    FDate = item.Date,
                    FSUPPLIERID = new Dto.Qtrkd.FSUPPLIERID()
                    {
                        FNumber = ""
                    },
                    FDEPTID = new FDEPTID()
                    {
                        FNumber = issue.Des_StockID
                    },
                    FACCEPTANCE = new FACCEPTANCE()
                    {
                        FStaffNumber = ""
                    },
                    FSTOCKERID = new Dto.Qtrkd.FSTOCKERID()
                    {
                        FNumber = ""
                    },
                    FSTOCKERGROUPID = new Dto.Qtrkd.FSTOCKERGROUPID()
                    {
                        FNumber = ""
                    },
                    FOwnerTypeIdHead = "BD_OwnerOrg",
                    FOwnerIdHead = new Dto.Qtrkd.FOwnerIdHead()
                    {
                        FNumber = KingdeeConfigConst.ORGID
                    },
                    FNOTE = item.Remark,
                    FBaseCurrId = new Dto.Qtrkd.FBaseCurrId()
                    {
                        FNumber = ""
                    },
                    FScanBox = "",
                    F_GDRJ_Base = new Dto.Qtrkd.F_GDRJ_Base()
                    {
                        FNUMBER = "RKLX0022"
                    },
                    F_GDRJ_PrintTimes = 0,
                    F_GDRJ_PrintDateTime = "",
                    F_GDRJ_UserId = new Dto.Qtrkd.F_GDRJ_UserId()
                    {
                        FUserID = ""
                    },
                    F_GDRJ_Datetime = "",
                    FEntity = new List<Dto.Qtrkd.FEntityItem>()
                }
            };
            foreach (var el in item.FG_InvDetail)
            {
                var child = new Dto.Qtrkd.FEntityItem()
                {
                    FEntryID = 0,
                    FInStockType = "",
                    FMATERIALID = new FMATERIALID()
                    {
                        FNumber = el.Inventory_ID
                    },
                    FAuxPropId = new Dto.Qtrkd.FAuxPropId()
                    {
                        FAUXPROPID__FF100002 = new()
                        {
                            FNumber = el.JarModelID //包装物及包装规格
                        },
                        FAUXPROPID__FF100003 = new()
                        {
                            FNumber = el.CF02  //出胶规格
                        },
                        FAUXPROPID__FF100004 = new()
                        {
                            FNumber = el.CF03  //地台板规格
                        }
                    },
                    FUnitID = new Dto.Qtrkd.FUnitID()
                    {
                        FNumber = el.FG_Inventory is not null ? el.FG_Inventory.Unit : "GONGJIN01"
                    },
                    FSTOCKID = new FSTOCKID()
                    {
                        FNumber = item.DesStock_ID
                    },
                    FStockLocId = new Dto.Qtrkd.FStockLocId()
                    {
                        FSTOCKLOCID__FF100006 = new()
                        {
                            FNumber = el.Stk_ID
                        }
                    },
                    FSTOCKSTATUSID = new FSTOCKSTATUSID()
                    {
                        FNumber = "KCZT01_SYS"
                    },
                    FLOT = new FLOT()
                    {
                        FNumber = issue.SummaryProject
                    },
                    FQty = el.Actual_Qty,
                    FBaseQty = el.Actual_Qty,
                    FEntryNote = "",
                    FBOMID = new FBOMID()
                    {
                        FNumber = ""
                    },
                    FPRODUCEDATE = "",
                    FMTONO = "",
                    FExtAuxUnitId = new Dto.Qtrkd.FExtAuxUnitId()
                    {
                        FNumber = ""
                    },
                    FExtAuxUnitQty = 0,
                    FProjectNo = "",
                    FOWNERTYPEID = "BD_OwnerOrg",
                    FOWNERID = new FOWNERID()
                    {
                        FNumber = KingdeeConfigConst.ORGID
                    },
                    FKEEPERTYPEID = "BD_KeeperOrg",
                    FKEEPERID = new FKEEPERID()
                    {
                        FNumber = KingdeeConfigConst.ORGID
                    },
                    FSerialSubEntity = new List<Dto.Qtrkd.FSerialSubEntityItem>() {
                        new Dto.Qtrkd.FSerialSubEntityItem(){
                            FDetailID = 0,
                            FSerialNo = "",
                            FSerialNote = ""
                        }
                    },
                    FSRCBILLNO = issue.No.Split("_")[0],
                    FSRCBILLTYPEID = "PRD_MO",
                    FEntity_Link = new List<Dto.Qtrkd.FEntity_LinkItem>() {
                        new Dto.Qtrkd.FEntity_LinkItem() {
                            FEntity_Link_FRuleId= "8dd42d28-bc69-4be7-9880-45b191989fe7",
                            FEntity_Link_FSTableName= "T_PRD_MOENTRY",
                            FEntity_Link_FSBillId= pRD_MO.FID,
                            FEntity_Link_FSId= pRD_MO.FTreeEntity_FEntryId,
                            FEntity_Link_FBASEQTYOld= el.Actual_Qty,
                            FEntity_Link_FBASEQTY= el.Actual_Qty

                        }
                    }

                };
                obj.Model.FEntity.Add(child);
            }
            return obj;
        }
        #endregion

        #region 工单回填生成其他出库单保存json
        /// <summary>
        /// 工单回填生成其他出库单保存json
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static object GdhtBuildQtckdJson(MaterialIssuePlan item)
        {
            STK_MisDelivery obj = new()
            {
                NeedUpDateFields = new List<string>(),
                NeedReturnFields = new List<string>(),
                IsDeleteEntry = "true",
                SubSystemId = "",
                IsVerifyBaseDataField = "false",
                IsEntryBatchFill = "true",
                ValidateFlag = "true",
                NumberSearch = "true",
                InterationFlags = "",
                Model = new Dto.Qtckd.Model()
                {
                    FID = 0,
                    FBillNo = item.No.Replace("MO", "MQT"),
                    FBillTypeID = new Dto.Qtckd.FBillTypeID()
                    {
                        FNUMBER = "QTCKD01_SYS"
                    },
                    FStockOrgId = new Dto.Qtckd.FStockOrgId()
                    {
                        FNumber = KingdeeConfigConst.ORGID
                    },
                    FPickOrgId = new FPickOrgId()
                    {
                        FNumber = KingdeeConfigConst.ORGID
                    },
                    FStockDirect = "GENERAL",
                    FDate = item.ActFinDate,
                    FCustId = new FCustId()
                    {
                        FNumber = ""
                    },
                    FDeptId = new Dto.Qtckd.FDeptId()
                    {
                        FNumber = item.Des_StockID
                    },
                    FPickerId = new Dto.Qtckd.FPickerId()
                    {
                        FStaffNumber = ""
                    },
                    FStockerId = new FStockerId()
                    {
                        FNumber = ""
                    },
                    FStockerGroupId = new FStockerGroupId()
                    {
                        FNumber = ""
                    },
                    FBizType = "",
                    FOwnerTypeIdHead = "BD_OwnerOrg",
                    FOwnerIdHead = new Dto.Qtckd.FOwnerIdHead()
                    {
                        FNumber = KingdeeConfigConst.ORGID
                    },
                    FNote = item.Summary,
                    FBaseCurrId = new Dto.Qtckd.FBaseCurrId()
                    {
                        FNumber = "PRE001"
                    },
                    FScanBox = "",
                    F_GDRJ_Base = new Dto.Qtckd.F_GDRJ_Base()
                    {
                        FNUMBER = "CKLX0002"
                    },
                    F_GDRJ_PrintTimes = 0,
                    F_GDRJ_PrintDateTime = "1900-01-01",
                    F_GDRJ_Base1 = new F_GDRJ_Base1()
                    {
                        FSTAFFNUMBER = ""
                    },
                    F_GDRJ_Base2 = new F_GDRJ_Base2()
                    {
                        FSTAFFNUMBER = ""
                    },
                    F_GDRJ_Text = "",
                    F_GDRJ_KH = new F_GDRJ_KH()
                    {
                        FNUMBER = ""
                    },
                    F_GDRJ_XSZZ = new F_GDRJ_XSZZ()
                    {
                        FNumber = ""
                    },
                    F_GDRJ_JHDD = new F_GDRJ_JHDD()
                    {
                        FNUMBER = ""
                    },
                    F_GDRJ_UserId = new Dto.Qtckd.F_GDRJ_UserId()
                    {
                        FUserID = ""
                    },
                    F_GDRJ_Datetime = "1900-01-01",
                    FEntity = new List<Dto.Qtckd.FEntityItem>()
                }
            };
            foreach (var batchItem in item.FG_MaterialIssueDetailsBatch)
            {
                var invDetail = item.FG_MaterialIssueDetails.Find(x => x.NOID == batchItem.NOID && x.Inventory_ID == batchItem.Inventory_ID);
                var child = new Dto.Qtckd.FEntityItem()
                {
                    FEntryID = 0,
                    FMaterialId = new Dto.Qtckd.FMaterialId()
                    {
                        FNumber = batchItem.Inventory_ID
                    },
                    FAuxPropId = new Dto.Qtckd.FAuxPropId()
                    {
                        FAUXPROPID__FF100002 = new Dto.Qtckd.FAUXPROPID__FF100002()
                        {
                            FNumber = ""
                        },
                        FAUXPROPID__FF100003 = new Dto.Qtckd.FAUXPROPID__FF100003()
                        {
                            FNumber = ""
                        },
                        FAUXPROPID__FF100004 = new Dto.Qtckd.FAUXPROPID__FF100004()
                        {
                            FNumber = ""
                        }
                    },
                    FUnitID = new Dto.Qtckd.FUnitID()
                    {
                        FNumber = batchItem.FG_Inventory is not null ? batchItem.FG_Inventory.Unit : "GONGJIN01"
                    },
                    FQty = batchItem.QTY,
                    FBaseUnitId = new Dto.Qtckd.FBaseUnitId()
                    {
                        FNumber = batchItem.FG_Inventory is not null ? batchItem.FG_Inventory.Unit : "GONGJIN01"
                    },
                    F_GDRJ_Integer = 0,
                    F_GDRJ_Text1 = "",
                    FStockId = new Dto.Qtckd.FStockId()
                    {
                        FNumber = batchItem.FG_Inventory is not null ? batchItem.FG_Inventory.StockID : "BJ01"
                    },
                    FStockLocId = new Dto.Qtckd.FStockLocId(),
                    FLot = new Dto.Qtckd.FLot()
                    {
                        FNumber = batchItem.ProjectBatch
                    },
                    FOwnerTypeId = "BD_OwnerOrg",
                    FPRODUCTGROUPID = new FPRODUCTGROUPID()
                    {
                        FNUMBER = ""
                    },
                    FOwnerId = new Dto.Qtckd.FOwnerId()
                    {
                        FNumber = KingdeeConfigConst.ORGID
                    },
                    FBomId = new Dto.Qtckd.FBomId()
                    {
                        FNumber = ""
                    },
                    FEntryNote = "",
                    FProjectNo = "",
                    FProduceDate = "",
                    FServiceContext = "",
                    FStockStatusId = new Dto.Qtckd.FStockStatusId()
                    {
                        FNumber = "KCZT01_SYS"
                    },
                    FMtoNo = "",
                    FCostItem = new FCostItem()
                    {
                        FNUMBER = ""
                    },
                    FKeeperTypeId = "BD_KeeperOrg",
                    FDistribution = "false",
                    FKeeperId = new Dto.Qtckd.FKeeperId()
                    {
                        FNumber = KingdeeConfigConst.ORGID
                    },
                    FExtAuxUnitId = new Dto.Qtckd.FExtAuxUnitId()
                    {
                        FNumber = ""
                    },
                    FExtAuxUnitQty = 0,
                    FSrcBillTypeId = "PRD_PPBOM",
                    FSrcBillNo = invDetail.Summary.Split(";")[2],
                    FSerialSubEntity = new List<Dto.Qtckd.FSerialSubEntityItem>() {
                         new Dto.Qtckd.FSerialSubEntityItem (){
                            FDetailID= 0,
                            FSerialNo = "",
                            FSerialNote= ""
                        }
                    },
                    FEntity_Link = new List<FEntity_Link>()
                    {
                        new FEntity_Link()
                        {
                            FEntity_Link_FFlowId= "",
                            FEntity_Link_FFlowLineId= 0,
                            FEntity_Link_FRuleId= "9eafedc7-de9f-41b7-8614-8f5a599c6c2e",
                            FEntity_Link_FSTableName= "T_PRD_PPBOMENTRY",
                            FEntity_Link_FSBillId= invDetail.Summary.Split(";")[0],
                            FEntity_Link_FSId= invDetail.Summary.Split(";")[1],
                            FEntity_Link_FBaseActualQtyOld= batchItem.QTY,//原始携带量   ---生产领料单的实发数量
                            FEntity_Link_FBaseActualQty= batchItem.QTY //修改携带量   ---生产领料单的实发数量
                        }
                    }
                };
                obj.Model.FEntity.Add(child);
            }
            return obj;
        }
        #endregion
    }
}
